#!/bin/bash

echo "Esperando a la configuración de red"
sleep 5

if grep -q "cd_unstable" /proc/cmdline; then
	PAQUETE_CLIENTE=unstable-ceutdeploy.tar.gz
else
	PAQUETE_CLIENTE=ceutdeploy.tar.gz
fi

if grep -q "cd_server" /proc/cmdline; then
	cd_server=`cat /proc/cmdline | awk -F= -v RS=" " '/cd_server/ {print $2}'`
	echo "Usando servidor ceutdeploy ${cd_server}..."
	SERVIDOR_DEPLOY=${cd_server}
	PATH_BOOT=ceutdeploy/boot

else
	echo "Sin servidor ceutdeploy definido en el arranque..."
	SERVIDOR_DEPLOY=ceutdeploy.googlecode.com
	PATH_BOOT=files
fi

cd /root

echo "Descargando Ceutdeploy..."
wget http://${SERVIDOR_DEPLOY}/${PATH_BOOT}/${PAQUETE_CLIENTE}
if [ -f ${PAQUETE_CLIENTE} ]; then
	mv ceutdeploy ceutdeploy.old
	tar zxvf ${PAQUETE_CLIENTE}
fi

if [ ! -z "${cd_server}" ]; then
	echo "Descargando y ejecutando autorun.sh..."
	wget http://${SERVIDOR_DEPLOY}/${PATH_BOOT}/autorun.sh
fi

if [ -f ./autorun.sh ]; then
	chmod u+x ./autorun.sh
	. ./autorun.sh
fi
